//
//  YearMonthDataPickview.m
//  PropertyManager
//
//  Created by 楠 on 2018/10/21.
//  Copyright © 2018年 Tng. All rights reserved.
//

#import "YearMonthDataPickview.h"

@implementation YearMonthDataPickview

+(instancetype)shareView
{
    static YearMonthDataPickview * view = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        view = [[YearMonthDataPickview alloc] initWithFrame:CGRectMake(0, 0 , WIDTH, HEIGHT)];
    });
    return view;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = SFM239;
        [self addSubview:self.bgView];
        [self.bgView addSubview:self.cancleBtn];
        [self.bgView addSubview:self.sureBtn];
        [self.bgView addSubview:self.pickview];
        
        NSDate *currentDate = [NSDate date];
        NSCalendar * calendar = [NSCalendar currentCalendar];
        NSDateComponents * components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate];
        NSInteger year = [components year];
        NSInteger month = [components month];

        [self.pickview selectRow:(year - 2000) inComponent:0 animated:NO];
        [self.pickview selectRow:(month - 1) inComponent:1 animated:NO];
    }
    return self;
}

-(UIView *)bgView
{
    if(_bgView == nil)
    {
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT - kHomeLineH - 216 - 44, WIDTH,  260)];
        _bgView.backgroundColor = SFMWhiteColor;
    }
    return _bgView;
}

-(UIPickerView *)pickview
{
    if (_pickview == nil) {
        _pickview = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, WIDTH, 216)];
        _pickview.dataSource = self;
        _pickview.delegate = self;
    }
    return _pickview;
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) {
        return self.yearDataArr.count;
    }else
    {
        return self.mouthDataArr.count;
    }
}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 44;
}

-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    return WIDTH/2;
}

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    if (!view){
        view = [[UIView alloc] init];
    }
    
    UILabel *text = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, WIDTH/2, 44)];
    text.textAlignment = NSTextAlignmentCenter;
    text.font = font(14);
    [view addSubview:text];
    
    if (component == 0) {
        text.text = [self.yearDataArr objectAtIndex:row];
    }else
    {
        text.text = [self.mouthDataArr objectAtIndex:row];
    }
    [self.pickview.subviews objectAtIndex:1].backgroundColor = [UIColor clearColor];
    [self.pickview.subviews objectAtIndex:2].backgroundColor = [UIColor clearColor];
    return view;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
}

-(NSArray *)yearDataArr
{
    if (_yearDataArr == nil) {
        NSMutableArray * arr = [NSMutableArray array];
        for (int i =2000; i<2100;i++ ) {
            [arr addObject:[NSString stringWithFormat:@"%d",i]];
        }
        _yearDataArr = arr;
    }
    return _yearDataArr;
}

-(NSArray *)mouthDataArr
{
    if (_mouthDataArr == nil) {
        _mouthDataArr = [NSArray arrayWithObjects:@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12", nil];
    }
    return _mouthDataArr;
}

-(void)hiddenView
{
    [UIView animateWithDuration:0.5 animations:^{
        [self removeFromSuperview];
    }];
}

-(void)showView
{
    [UIView animateWithDuration:0.5 animations:^{
        [[UIApplication sharedApplication].keyWindow addSubview:self];
    }];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self hiddenView];
}

-(UIButton *)cancleBtn
{
    if (_cancleBtn == nil) {
        _cancleBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
        [_cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancleBtn setTitleColor:SFM153 forState:UIControlStateNormal];
        [_cancleBtn addTarget:self action:@selector(cancleBtnClcik) forControlEvents:UIControlEventTouchUpInside];
        _cancleBtn.titleLabel.font = font(16);
    }
    return _cancleBtn;
}

-(void)cancleBtnClcik
{
    [self hiddenView];
}

-(UIButton *)sureBtn
{
    if (_sureBtn == nil) {
        _sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(WIDTH - 60, 0, 60, 44)];
        [_sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_sureBtn setTitleColor:SFMBlueColor forState:UIControlStateNormal];
        [_sureBtn addTarget:self action:@selector(sureBtnClcik) forControlEvents:UIControlEventTouchUpInside];
        _sureBtn.titleLabel.font = font(16);
    }
    return _sureBtn;
}

-(void)sureBtnClcik
{
    NSInteger section = [self.pickview selectedRowInComponent:0];
    NSInteger row = [self.pickview selectedRowInComponent:1];
    NSString * year = [self.yearDataArr objectAtIndex:section];
    NSString * month = [self.mouthDataArr objectAtIndex:row];
    self.sendYearMonthDayBlcok(year, month);
    [self hiddenView];
}

@end
